<?php
namespace app\admin\controller;

/**
 * Class SchoolRanking
 * @package app\admin\controller
 * 在校排名控制器
 */
class SchoolRanking extends Basic
{

    /**
     * 初始化
     */
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 添加在校排名
     */
    public function add_ranking(){
        $name = input('name');
        $sort = input('sort',1);
        $status = input('status',1);
        if (!$name) {
            json_fail('缺少标题参数');
        }
        //实例化数据库
        $school_ranking = new \app\admin\model\SchoolRanking();

        //要添加的数据
        $addData = array(
            'name'=>$name,
            'sort'=>$sort,
            'create_time'=>time(),
            'status'=>$status,
        );

        $ranking_id = $school_ranking->insertGetId($addData);
        if(!$ranking_id){
            json_fail('存储失败');
        }
        json_success($ranking_id);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 在校排名列表
     * @author chengkaikai
     */
    public function ranking_list(){
        $keys = input('keys');
        $page = input('page',1);
        $limit = input('limit',10);

        //实例化数据库
        $school_ranking = new \app\admin\model\SchoolRanking();

        $map['status'] = array('neq',0);
        if($keys){
            $map['name']=array('like','%'.$keys.'%');
        }

        $list = $school_ranking->where($map)->order('sort')->page($page,$limit)->select();
        if(!$list){
            $infoList = array(
                'list'=>[],
                'count'=>0
            );
            json_success($infoList);
        }
        $infoList = array(
            'list'=>$list,
            'count'=>$school_ranking->where($map)->count()
        );
        json_success($infoList);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author chengkaikai
     * 展示修改
     */
    public function show_edit(){
        $ranking_id = input('ranking_id');
        if (!$ranking_id) {
            json_fail('缺少排名id参数');
        }

        //实例化数据库
        $school_ranking = new \app\admin\model\SchoolRanking();

        $info = $school_ranking->where(array('id'=>$ranking_id))->find();

        if(!$info){
            json_fail('暂无数据');
        }
        json_success($info);
    }

    /**
     * 修改专业
     * @author chengkaikai
     */
    public function edit_ranking(){
        $name = input('name');
        $sort = input('sort',1);
        $status = input('status',1);
        $ranking_id = input('ranking_id');
        if (!$ranking_id) {
            json_fail('缺少排名id参数');
        }
        if (!$name) {
            json_fail('缺少标题参数');
        }
        //实例化数据库
        $school_ranking = new \app\admin\model\SchoolRanking();

        //要修改的数据
        $saveData = array(
            'name'=>$name,
            'sort'=>$sort,
            'status'=>$status,
        );

        $info = $school_ranking->where(array('id'=>$ranking_id))->update($saveData);
        if($info === false){
            json_fail('存储失败');
        }
        json_success('修改成功');
    }

    /**
     * 删除排名
     * @author chengkaikai
     */
    public function delete_ranking(){
        $ranking_id = input('ranking_id');
        if (!$ranking_id) {
            json_fail('缺少排名id参数');
        }

        //实例化数据库
        $school_ranking = new \app\admin\model\SchoolRanking();

        //要修改的数据
        $saveData = array(
            'status'=>0
        );

        $info = $school_ranking->where(array('id'=>$ranking_id))->update($saveData);
        if($info === false){
            json_fail('删除失败');
        }
        json_success('删除成功');
    }
}